home *** CD-ROM | disk | FTP | other *** search
/ 3D Game Programming All in One / 3D Game Programming All in One Disc.iso / 3D2E / RESOURCES / CH4 / EMAGA4 BOOK CODE / main.cs
Text File  |  2006-09-21  |  6KB  |  151 lines

  1. $traceMode = false;
  2. $winConsole = true;
  3.  
  4. trace($traceMode);//leave this as is,change $traceMode above to true/false instead.
  5. EnableWinConsole($winConsole); // leave alone, change $winConsole instead.
  6. // send logging output to a windows console window
  7. //------------------------------------------------------------------------
  8. //  ./main.cs   v0.3
  9. //
  10. //  root main module for 3D2E emaga4 sample game
  11. //
  12. //  Copyright (c) 2003, 2006 by Kenneth C.  Finney.
  13. //------------------------------------------------------------------------
  14.  
  15. // ========================================================================
  16. // ========================= Initializations ==============================
  17. // ========================================================================
  18.  
  19. $usageFlag =  false;  //help won't be displayed unless the command line
  20.                       //switch ( -h ) is used
  21.  
  22. $logModeEnabled =  true; //track the logging state we set in the next line.
  23. SetLogMode(2);   // overwrites existing logfile & closes log file at exit.
  24.  
  25. // ========================================================================
  26. // ======================= Function Definitions ===========================
  27. // ========================================================================
  28.  
  29. function OnExit()
  30. //------------------------------------------------------------------------
  31. // This is called from the common code modules. Any last gasp exit
  32. // acticities we might want to perform can be put in this function.
  33. // We need to provide a stub to prevent warnings in the log file.
  34. //------------------------------------------------------------------------
  35. {
  36. }
  37.  
  38. function OnStart()
  39. //------------------------------------------------------------------------
  40. // This is called from the common code modules. Any last initial
  41. // acticvities we might want to perform can be put in this function.
  42. // We need to provide a stub to prevent warnings in the log file.
  43. //------------------------------------------------------------------------
  44. {
  45. }
  46.  
  47. function ParseArgs()
  48. //------------------------------------------------------------------------
  49. //  handle the command line arguments
  50. //
  51. //  this function is called from the common code
  52. // NOTE: some variables used in the function are globals (use the $)
  53. // because we need their values that are set inside this function to 
  54. // also be accessible outside this function as well.
  55. //------------------------------------------------------------------------
  56. {
  57.   for(%i = 1; %i  < $Game::argc ; %i++) //loop thru all command line args
  58.   {
  59.     %currentarg    = $Game::argv[%i];   // get current arg from the list
  60.     %nextArgument       = $Game::argv[%i+1]; // get arg after the current one
  61.     %nextArgExists = $Game::argc-%i > 1;// if there *is* a next arg,note that
  62.     $logModeEnabled =  false;           // turn this off-let the args dictate
  63.                                 // if logging should be enabled.
  64.  
  65.     switch$(%currentarg)
  66.     {
  67.       case "-?":       // the user wants command line help, so this cause the
  68.         $usageFlag = true;   // Usage function to be run, instead of the game
  69.         $argumentFlag[%i] = true;                // adjust the argument count
  70.  
  71.       case "-h":         // exactly the same as "-?"
  72.         $usageFlag = true;
  73.         $argumentFlag[%i] = true;
  74.     }
  75.   }
  76. }
  77.  
  78. function Usage()
  79. //------------------------------------------------------------------------
  80. // Display the command line usage help
  81. //------------------------------------------------------------------------
  82. {
  83. //  NOTE: any logging entries are written to the file 'console.log'
  84.   Echo("\n\nemaga4 command line options:\n\n" @
  85.          " -h, -?              display this message\n" );
  86. }
  87.  
  88. function  LoadAddOns(%list)
  89. //------------------------------------------------------------------------
  90. // Exec each of the startup scripts for add-ons.
  91. //------------------------------------------------------------------------
  92. {
  93.   if (%list $= "")
  94.     return;
  95.   %list = NextToken(%list, token, ";");
  96.   LoadAddOns(%list);
  97.   Exec(%token @  "/main.cs");
  98. }
  99.  
  100. // ========================================================================
  101. // ================ Module Body - Inline Statements =======================
  102. // ========================================================================
  103. //  Parse the command line  arguments
  104. ParseArgs();
  105.  
  106. //  Either  display the help message or start the program.
  107. if  ($usageFlag)
  108. {
  109.   %saveWinConsole = $winConsole;
  110.   EnableWinConsole(false);// send logging output to a windows console window
  111.   Usage();
  112.   EnableWinConsole(%saveWinConsole);//restore windows console mode
  113.   Quit();
  114. }
  115. else
  116. {
  117.  
  118.   //  scan argument list, and log an Error message for each unused argument
  119.   // NOTE that the $i is used, not %i. This code is NOT in a function - it is inline,
  120.   // so we need to use the $ (global scope) and not the % (local scope).
  121.   for ($i = 1;  $i  < $Game::argc; $i++)
  122.   {
  123.      if (!$argumentFlag[$i])
  124.         Error("Error: Unknown command line argument:  " @ $Game::argv[$i]);
  125.   }
  126.  
  127.   if  (!$logModeEnabled)
  128.   {
  129.      SetLogMode(6);      //  Default to  a new logfile each session.
  130.   }
  131.   //  Set the add-on path list to specify the directories that will be
  132.   //  available to the scripts and engine. note that *all* required
  133.   //  directory trees are included: common and control as well as the
  134.   //  user add-ons.
  135.   $pathList = $addonList !$= "" ? $addonList @ ";control;common" : "control;common";
  136.   SetModPaths($pathList);
  137.  
  138.   // Execute startup script for the common code modules
  139.   Exec("common/main.cs");
  140.  
  141.   // Execute startup script for the control specific code modules
  142.   Exec("control/main.cs");
  143.  
  144.   // Execute startup scripts for all user add-ons
  145.   Echo("--------- Loading Add-ons ---------");
  146.   LoadAddOns($addonList);
  147.   Echo("Engine initialization complete.");
  148.  
  149.   OnStart();
  150. }
  151.